{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "1ff8ab56",
   "metadata": {},
   "source": [
    "# 上周实践回顾"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "b45ef0f4",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests   # 导入模块"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "9f14ad7c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1. url\n",
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_key = 'Ky4IfkD-8kEJCuju2IFT1WcpUI_omfjy'\n",
    "API_secret = 'Z6RK-Fw8TTrMIle4ZgOX9hz-JSfyGGeT'\n",
    "\n",
    "# 3. 调用请求方法 POST\n",
    "\n",
    "# 4. 所有 API Key 都可以调用本 API\n",
    "#    其中 calculate_all 和 face_rectangle 参数只有正式 API Key 才能使用，试用 API Key 无法使用\n",
    "\n",
    "# 5. payload： 酬载, 必要阅读api文档中的 *必选项* 和 *可选性*，及 *参数说明*\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_secret,\n",
    "    'image_url':'https://tse1-mm.cn.bing.net/th/id/OIP-C.HPWFQ7UDQh93EKcDmiUQ_wHaIR?w=176&h=197&c=7&r=0&o=5&dpr=2&pid=1.7'    \n",
    "}\n",
    "\n",
    "r = requests.post(base_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "5642ad02",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "2b550bbb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647345120,f1565813-8475-4290-914e-ee66c1a32144',\n",
       " 'time_used': 530,\n",
       " 'faces': [{'face_token': '4f1d074978a129203ebad36eeee173d9',\n",
       "   'face_rectangle': {'top': 108, 'left': 141, 'width': 107, 'height': 107}}],\n",
       " 'image_id': 'JoYdaZ0h1hGb4ih1d2EZVw==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "12dcdd32",
   "metadata": {},
   "source": [
    "# 本周内容"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "907c101b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 人脸分析\n",
    "# 1. url\n",
    "face_detect_url = 'https://api-cn.faceplusplus.com/facepp/v3/face/analyze'\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_key = 'Ky4IfkD-8kEJCuju2IFT1WcpUI_omfjy'\n",
    "API_secret = 'Z6RK-Fw8TTrMIle4ZgOX9hz-JSfyGGeT'\n",
    "\n",
    "# 3. payload\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_secret,\n",
    "    'return_attributes':'gender,age,beauty,emotion',\n",
    "    'face_tokens':'4f1d074978a129203ebad36eeee173d9'    \n",
    "}\n",
    "\n",
    "r = requests.post(face_detect_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "eba372c3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "c824e4ba",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 148,\n",
       " 'request_id': '1647345509,6eb7202d-ba0d-40dc-b0d7-0c8069ac372e',\n",
       " 'faces': [{'attributes': {'emotion': {'sadness': 0.039,\n",
       "     'neutral': 0.001,\n",
       "     'disgust': 0.191,\n",
       "     'anger': 0.001,\n",
       "     'surprise': 0.031,\n",
       "     'fear': 0.001,\n",
       "     'happiness': 99.736},\n",
       "    'gender': {'value': 'Male'},\n",
       "    'age': {'value': 53},\n",
       "    'beauty': {'female_score': 58.555, 'male_score': 58.212}},\n",
       "   'face_rectangle': {'width': 107, 'top': 108, 'left': 141, 'height': 107},\n",
       "   'face_token': '4f1d074978a129203ebad36eeee173d9'}]}"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "db3b4b93",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "3c128673",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>face_token</th>\n",
       "      <td>4f1d074978a129203ebad36eeee173d9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.sadness</th>\n",
       "      <td>0.039</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.neutral</th>\n",
       "      <td>0.001</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.disgust</th>\n",
       "      <td>0.191</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.anger</th>\n",
       "      <td>0.001</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.surprise</th>\n",
       "      <td>0.031</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.fear</th>\n",
       "      <td>0.001</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.happiness</th>\n",
       "      <td>99.736</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.gender.value</th>\n",
       "      <td>Male</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.age.value</th>\n",
       "      <td>53</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.beauty.female_score</th>\n",
       "      <td>58.555</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.beauty.male_score</th>\n",
       "      <td>58.212</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.width</th>\n",
       "      <td>107</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.top</th>\n",
       "      <td>108</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.left</th>\n",
       "      <td>141</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.height</th>\n",
       "      <td>107</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                               0\n",
       "face_token                      4f1d074978a129203ebad36eeee173d9\n",
       "attributes.emotion.sadness                                 0.039\n",
       "attributes.emotion.neutral                                 0.001\n",
       "attributes.emotion.disgust                                 0.191\n",
       "attributes.emotion.anger                                   0.001\n",
       "attributes.emotion.surprise                                0.031\n",
       "attributes.emotion.fear                                    0.001\n",
       "attributes.emotion.happiness                              99.736\n",
       "attributes.gender.value                                     Male\n",
       "attributes.age.value                                          53\n",
       "attributes.beauty.female_score                            58.555\n",
       "attributes.beauty.male_score                              58.212\n",
       "face_rectangle.width                                         107\n",
       "face_rectangle.top                                           108\n",
       "face_rectangle.left                                          141\n",
       "face_rectangle.height                                        107"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.json_normalize(r.json()['faces'][0]).T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "875c5a20",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 人脸对比\n",
    "# 1. url\n",
    "base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "\n",
    "\n",
    "# 2. api账户/通行证\n",
    "API_key = 'Ky4IfkD-8kEJCuju2IFT1WcpUI_omfjy'\n",
    "API_secret = 'Z6RK-Fw8TTrMIle4ZgOX9hz-JSfyGGeT'\n",
    "\n",
    "# 3. 调用请求方法 POST\n",
    "\n",
    "# 4. 所有 API Key 都可以调用本 API\n",
    "#    其中 calculate_all 和 face_rectangle 参数只有正式 API Key 才能使用，试用 API Key 无法使用\n",
    "\n",
    "# 5. payload： 酬载, 必要阅读api文档中的 *必选项* 和 *可选性*，及 *参数说明*\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_secret,\n",
    "    'image_url':'https://img.kuyv.cn/202011/07/165359721.jpg'    \n",
    "}\n",
    "\n",
    "r = requests.post(base_url, params = payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "29ce63c3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "1cf07ecb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647346901,64c4e6aa-00c4-4a45-9490-ef7d0647987b',\n",
       " 'time_used': 550,\n",
       " 'faces': [{'face_token': 'de54d41a12e855304ff07c12729583a3',\n",
       "   'face_rectangle': {'top': 231, 'left': 323, 'width': 313, 'height': 313}}],\n",
       " 'image_id': 'aBgODNUGne9Or5b4Mv9ZIg==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "6f471185",
   "metadata": {},
   "outputs": [],
   "source": [
    "liu_face01 = '4f1d074978a129203ebad36eeee173d9'\n",
    "liu_face02 = 'de54d41a12e855304ff07c12729583a3'\n",
    "\n",
    "face_compare_url = 'https://api-cn.faceplusplus.com/facepp/v3/compare'\n",
    "\n",
    "payload = {\n",
    "    'api_key':API_key,\n",
    "    'api_secret':API_secret,\n",
    "    'face_token1':liu_face01,\n",
    "    'face_token2':liu_face02\n",
    "}\n",
    "r = requests.post(url=face_compare_url,params=payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "b0801d2e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "dca639a3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647347270,b7ab6f0f-8e11-41fc-ab8b-da0d18020d16',\n",
       " 'time_used': 434,\n",
       " 'confidence': 80.142,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-4': 69.101, '1e-5': 73.975}}"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b3e2b763",
   "metadata": {},
   "source": [
    "# 人脸检测函数封装"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "id": "de9daf49",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 函数封装，为了更便捷的调用 API\n",
    "def face_detect(API_key,API_secret,image_url,landmark=None,attributes=None,face_rectangle=None,beauty_score_min=0,beauty_score_max=100):\n",
    "    \"\"\"face++ 人脸检测接口函数(图片链接)\"\"\"\n",
    "    face_detect_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "    payload = {\n",
    "        'api_key':API_key,\n",
    "        'api_secret':API_secret,\n",
    "        'image_url':image_url,\n",
    "        'return_landmark':landmark,\n",
    "        'return_attributes':attributes,\n",
    "        'face_rectangle':face_rectangle,\n",
    "        'beauty_score_min':beauty_score_min,\n",
    "        'beauty_score_max':beauty_score_max\n",
    "    }\n",
    "    r = requests.post(face_detect_url,payload)\n",
    "    return r.json()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "id": "cba16ca7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1647429899,1f79dd05-9d6d-4219-bc66-72b2e8a12337',\n",
       " 'time_used': 1406,\n",
       " 'error_message': 'INVALID_IMAGE_SIZE: image_url'}"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 函数调用\n",
    "\n",
    "API_key = 'Ky4IfkD-8kEJCuju2IFT1WcpUI_omfjy'\n",
    "API_secret = 'Z6RK-Fw8TTrMIle4ZgOX9hz-JSfyGGeT'\n",
    "image_url = 'https://tse1-mm.cn.bing.net/th/id/R-C.3043a5f57969752851cf91570750e666?rik=tS0BPbOhGV3sew&riu=http%3a%2f%2finews.gtimg.com%2fnewsapp_match%2f0%2f10351733955%2f0&ehk=a2tbDW5R0cH%2bYqK3TvHTWkM0zMZgZAHUTZWI5yazKUM%3d&risl=&pid=ImgRaw&r=0'\n",
    "\n",
    "face_detect(API_key,API_secret,image_url)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "68647181",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "51489eaf",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "571bc61e",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
